import { Tree } from "./Tree"
const tree2: Tree = {
  val: 'a',
  children: [
    {
      val: 'b',
      children: [
        {
          val: 'd',
          children: []
        },
        {
          val: 'e',
          children: []
        },
      ]
    },
    {
      val: 'c',
      children: [
        {
          val: 'f',
          children: []
        },
        {
          val: 'g',
          children: []
        },
      ]
    }
  ]
}

/**
 * 广度优先队列通过
 * @param root 根元素
 */
const bfs = (root: Tree) => {
  // 入队
  const q = [root]
  // 直到没有节点，退出循环
  while (q.length > 0) {
    // 出队
    const n = q.shift()
    console.log(n?.val)
    // 让孩子依次入队
    n?.children.forEach(child => {
      q.push(child)
    })
  }
}

bfs(tree2)